<?php
session_start();
require_once 'user21.php';

$user = new User();
$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $confirmPassword = $_POST['confirmPassword'];

        if ($password != $confirmPassword) {
            throw new Exception('两次输入的密码不一致');
        }

        $userId = $user->register($username, $password);
        $success = "注册成功! 请登录";

        header('Location: login21.php');
        exit;
    } catch (Exception $e) {
        $error = $e->getMessage();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教学服务系统 - 注册</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <div class="card" style="max-width: 400px; margin: 50px auto;">
            <div class="card-header">
                <h2>用户注册</h2>
            </div>

            <?php if (!empty($error)) : ?>
            <div class="message message-error">
                <?php echo $error; ?>
            </div>
            <?php endif; ?>

            <form method="post">
                <div class="form-group">
                    <label for="username" class="form-label">用户名</label>
                    <input type="text" name="username" id="username" class="form-control" required>
                </div>
                <div>
                    <label for="password" class="form-label">密码</label>
                    <input type="password" name="password" id="password" class="form-control" required>
                </div>
                <div>
                    <label for="confirmPassword" class="form-label">确认密码</label>
                    <input type="password" name="confirmPassword" id="confirmPassword" class="form-control" required>
                </div>

                <button type="submit" class="btn btn-success">注册</button>

                <div class="mt-3">
                    已有账号? <a href="login21.php">立即登录</a>
                </div>
            </form>
        </div>
    </div>
</body>
</html>